<template>
  <view class="page-wrapper">
    <nav-bar title="测试"></nav-bar>

    <view class="page-main">
      <scroll-view>
        <view class="page-container">
          <br />
          <button @click="down_2">上传本地文件</button>

          <br />
          <button @click="fn_02">get请求(不带参数)</button>

          <br />
          <button @click="fn_06">post(上传本地字符串)</button>

          <br />
          <button @click="createBarcode">扫码{{ info.platform }}</button>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script setup>
import utils from '@/common/js/utils/index.js'; // 引入公共方法
const { Custom_sendLog } = utils; // 引入公共方法
// console.log(27, Custom_sendLog);

const fileUrl = '/static/归档.zip';
// const fileUrl = '/static/解压.zip';

const info = reactive({
  platform: '', // 平台
});

// 上传本地实际文件
const down_2 = async () => {
  try {
    uni.uploadFile({
      url: 'http://172.17.230.47:3000/upload', // 上传接口地址
      filePath: fileUrl, // 本地文件路径
      name: 'file', // 文件字段名，与服务器接口一致
      // formData: {
      //   fileName: '移动端.zip', // 附加的表单数据
      // },
      success: (uploadResponse) => {
        console.log('Upload success:', uploadResponse);
        if (uploadResponse.statusCode === 200) {
          console.log('Server response:', uploadResponse.data);
        } else {
          console.error('Server error:', uploadResponse.data);
        }
      },
      fail: (error) => {
        console.error('Upload error:', error);
      },
    });
  } catch (error) {
    console.error('Error:', error);
  }
};

// get请求(不带参数)
const fn_02 = async () => {
  try {
    const response = await uni.request({
      url: 'http://172.17.230.47:3000/getno',
      method: 'GET',
    });
    if (response.statusCode === 200) {
      console.log('get返回值response', response.data);
    }
  } catch (error) {
    console.error('Error:', error);
  }
};

const str = `
const fs = require('fs');
const path = require('path');

`;

// post-上传本地代码(带参数)
const fn_06 = async () => {
  try {
    const codeString = JSON.stringify({
      str: str,
      fileName: '', // 传空的话按照时间戳存储在服务器端
    });

    uni.request({
      url: 'http://172.17.230.47:3000/uploadCode', // 服务端接口地址
      method: 'POST',
      data: codeString,
      header: {
        'Content-Type': 'application/json', // 设置请求头为 JSON
      },
      success: (res) => {
        console.log('Upload success:', res.data);
      },
      fail: (error) => {
        console.error('Upload error:', error);
      },
    });
  } catch (error) {
    console.error('Error:', error);
  }
};

// const createBarcode = () => {
//   let _this = this;
//   if (!_this.barcode) {
//     _this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
//       top: '0px',
//       left: '0px',
//       width: '100%',
//       height: '100%',
//       position: 'static',
//     });
//     _this.barcode.onmarked = function (type, result) {
//       console.log('Success: type=' + type + ', result=' + result);
//     };
//     plus.webview.currentWebview().append(_this.barcode);
//   }
//   _this.barcode.start();
// };

const createBarcode = () => {
  const systemInfo = uni.getSystemInfoSync();
  switch (systemInfo.platform) {
    case 'android':
      info.platform = 'android';
      console.log('运行在Android设备上');
      break;
    case 'ios':
      info.platform = 'ios';
      console.log('运行在iOS设备上');
      break;
    case 'h5':
      info.platform = 'h5';
      console.log('运行在H5环境中');
      break;
    default:
      info.platform = 'other';
      console.log('其他平台');
  }

  // 判断是否为PC浏览器
  const isPCBrowser = () => {
    return (
      systemInfo.platform === 'h5' && /Win|Mac|Linux/.test(navigator.userAgent)
    );
  };

  if (isPCBrowser()) {
    console.warn('当前运行在PC浏览器上，扫码功能不可用');
    return; // 退出函数，不执行扫码相关代码
  }

  console.log('创建扫码实例');

  // 检查plus对象是否存在
  if (typeof plus === 'undefined' || !plus.barcode) {
    console.error('plus.barcode 不可用，可能不是在5+ App环境中');
    return; // 退出函数
  }

  console.log('创建扫码实例');
  console.log('plus', plus.barcode);
  try {
    let barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
      top: '0px',
      left: '0px',
      width: '100%',
      height: '100%',
      position: 'static',
    });
    barcode.onmarked = function (type, result) {
      console.log('Success: type=' + type + ', result=' + result);
    };
    plus.webview.currentWebview().append(barcode);
    barcode.start();
  } catch (error) {
    console.error('Error:', error);
  }
};

/* plus.barcode 的打印
{
  "Barcode": "function() { [native code] }",
  "create": "function() { [native code] }",
  "getBarcodeById": "function() { [native code] }",
  "scan": "function() { [native code] }",
  "QR": 0,
  "EAN13": 1,
  "EAN8": 2,
  "AZTEC": 3,
  "DATAMATRIX": 4,
  "UPCA": 5,
  "UPCE": 6,
  "CODABAR": 7,
  "CODE39": 8,
  "CODE93": 9,
  "CODE128": 10,
  "ITF": 11,
  "MAXICODE": 12,
  "PDF417": 13,
  "RSS14": 14,
  "RSSEXPANDED": 15
}
*/
</script>

<style lang="scss" scoped></style>
